import { message } from "@/utils/message";
import BigNumber from "bignumber.js";
import { ElLoading } from "element-plus";
import { toRaw } from "vue";

export const numberFormat = number => {
  if (!number) {
    return "--";
  }
  number = number + "";

  const re = /^(-?\d+)(\d{3})(\.?\d*)/;
  while (re.test(number)) {
    number = number.replace(re, "$1,$2$3");
  }

  return number;
};

/**
 * treeData 分为上下级
 * @param treeData 原数据
 * @param parentIdKey
 * @param parentNamekey
 * @param itemNameKey
 * @returns
 */
export function dealTreeList(
  treeData: Array<any>,
  parentIdKey: string,
  parentNamekey: string,
  itemNameKey: string
) {
  let tempList = [];
  treeData.forEach(item => {
    let obj = tempList.find(v => v.id == item[parentIdKey]);
    if (obj) {
      obj.children.push({
        id: item.id,
        label: item[itemNameKey] || "--"
      });
    } else {
      tempList.push({
        id: item[parentIdKey],
        label: item[parentNamekey] || "--",
        children: [
          {
            id: item.id,
            label: item[itemNameKey] || "--"
          }
        ]
      });
    }
  });
  return tempList;
}

// 数据 * 100
export function dealDataRide(data, flag = 100, decimal = undefined) {
  if (!data) {
    return "";
  } else {
    data = new BigNumber(data);
    return decimal
      ? data.times(flag).toFormat(decimal)
      : data.times(flag).toNumber();
  }
}

// 数据 / 100
export function dealDataExcept(data, flag = 100, decimal = undefined) {
  if (!data) {
    return "";
  } else {
    data = new BigNumber(data);
    return decimal
      ? data.div(flag).toFormat(decimal)
      : data.div(flag).toNumber();
  }
}

// 处理相加
export function dealDataAdd(data, flag = 0) {
  if (!data) {
    return "";
  } else {
    data = new BigNumber(data);
    return data.plus(flag || 0).toNumber();
  }
}

// 处理相减
export function dealDataCut(data, flag = 0) {
  if (!data) {
    return "";
  } else {
    data = new BigNumber(data);
    return data.minus(flag || 0).toNumber();
  }
}

/**
 * 下载 Blob 数据
 * @param blobUrl - Blob 数据的 URL
 * @param fileName - 下载的文件名，如果未提供，则默认为 'file'
 * @returns 无返回值
 */
export function downloadBlob(blobUrl, fileName) {
  const a = document.createElement("a");
  a.href = blobUrl;
  a.download = fileName || "file";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(blobUrl);
}

/**
 * 导出并下载数据
 * @param form - 表单数据
 * @param pagination - 分页数据
 * @param api - 导出数据的 API 接口
 * @param name - 导出的文件名，如果未提供，则默认为 'file'
 * @returns 无返回值
 */
export async function ExportDownload(form, pagination, api, name = "file") {
  const loadingInstance = ElLoading.service({
    text: "正在下载数据，请稍候",
    background: "rgba(0, 0, 0, 0.7)"
  });

  const params = {
    ...toRaw(form),
    pageNum: pagination.currentPage,
    pageSize: 99999
  };

  const data = await api(params);

  if (data) {
    const blobFile = new Blob([data]);
    const blobUrl = URL.createObjectURL(blobFile);
    // 调用下载函数
    downloadBlob(blobUrl, name);
    loadingInstance.close();
  } else {
    message("导出失败", {
      type: "error"
    });
    loadingInstance.close();
  }
}

export function getIMPtemplate(str) {
  const BaseUrl = "/erpPlat/excel/";
  const tempList = {
    // 导入门店模板
    importStoreTem: BaseUrl + "门店导入模板.xls",
    // 导入预警金额模板
    importWarnTem: BaseUrl + "门店预警金额上限模版.xls",
    // 导入仓库模板
    importDeposTem: BaseUrl + "仓库导入模板.xls",
    // 导入供应商模板
    importSuppliTem: BaseUrl + "供应商导入模板.xls"
  };
  return tempList[str];
}

let commonUtil = {
  dealDataRide, // 数据乘 默认10 *10
  dealDataExcept, // 数据除 默认10 /10
  dealDataAdd, // 数据相加
  dealDataCut // 数据相减
};

export default commonUtil;
